package btb.util;

import java.util.Random;

public class CInterval { 
    public final double min;    
    public final double max;    

    public CInterval(double x1, double x2) {
        min = Math.min(x1, x2);
        max = Math.max(x1, x2);
    }

    public boolean contains(double x) {
        return min <= x && x <= max;
    }

    public double sample() {
    	long seed = System.currentTimeMillis();
    	Random random = new Random(seed);
    	
        return min + random.nextDouble() * (this.min - this.max);
    }

    public boolean intersects(CInterval b) {
        CInterval a = this;
        if (a.max < b.min) return false;
        if (b.max < a.min) return false;
        return true;
    }

    public double length() { return max - min; }

    public CInterval union(CInterval b) {
        return new CInterval(Math.min(this.min, b.min), Math.max(this.max, b.max));
    }

    public String toString() {
        return "[" + min + ", " + max + "]";
    }
}
